package com.easy.web.controller.common;

import com.alicp.jetcache.Cache;
import com.alicp.jetcache.anno.CreateCache;
import com.easy.common.constant.RegularExpression;
import com.easy.common.core.R;
import com.easy.common.core.domain.entity.User;
import com.easy.framework.UserVerify;
import com.easy.user.service.UserService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.concurrent.TimeUnit;

import static com.easy.common.constant.ResultConstant.*;

/**
 * 登录 校验 控制器
 *
 * @author RuoYu
 * @date 2022/11/23
 */
@RestController
@RequestMapping
@Api(tags = "登录校验控制器")
public class VerifyController {

    /*登录校验*/
    @Autowired
    private UserVerify userVerify;

    @Autowired
    private UserService userService;

    /**
     * 校验 用户是否存在
     *
     * @param account 账户
     * @return {@link R}
     */
    @GetMapping("v/check")
    @ApiOperation(value = "校验用户名是否存在")
    @ApiImplicitParam(name = "account", value = "登录账户", dataTypeClass = String.class, required = true)
    public R loginCheck(String account) {
        String msg = userVerify.loadUserByAccount(account);
        if (msg.equals("0")) {
            return R.success();
        }
        return R.error(msg);
    }

    /**
     * 检查电话或电子邮件是否存在
     *
     * @param account 独特
     * @return {@link R}
     */
    @GetMapping("v/account/{account}")
    @ApiOperation(value = "校验账户（手机号、邮箱号）是否存在")
    @ApiImplicitParam(name = "account", value = "账户", dataTypeClass = String.class, required = true)
    public R checkPhoneOrEmail(@PathVariable String account) {
        // 1.校验是手机号作为唯一标识还是邮箱号
        boolean phone = account.matches(RegularExpression.PHONE_PATTERN);
        boolean mail = account.matches(RegularExpression.MAIL_PATTERN);
        if (phone) {
            User user = userService.selectByUserPhone(account);
            // 2.通过手机号查询不到，代表该手机号未注册
            if (user == null) {
                return R.success();
            } else {
                return R.error(PHONE_BEEN_REGISTERED);
            }
        } else if (mail) {
            User user = userService.selectByUserEmail(account);
            // 2.通过邮箱号查询不到，代表该手机号未注册
            if (user == null) {
                return R.error(EMAIL_BEEN_REGISTERED);
            }
        }
        return R.error();
    }

    @CreateCache(area = "email", name = "ord_user_", expire = 300, timeUnit = TimeUnit.SECONDS)
    private Cache<String, String> jetCache;

    /**
     * 校验验证码是否正确
     *
     * @param account 账户
     * @param code    验证码
     * @return {@link R}
     */
/*    @GetMapping("v/account/{account}/{code}")
    @ApiOperation(value = "校验验证码是否正确")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "account", value = "账户", dataTypeClass = String.class, required = true),
            @ApiImplicitParam(name = "code", value = "验证码", dataTypeClass = String.class, required = true)
    })
    public R checkCode(@PathVariable String account, @PathVariable String code) {
        String redis_code = jetCache.get(account);
        if (redis_code.equals(code)) {
            return R.success();
        }
        return R.error(VERIFICATION_CODE_ERROR);
    }*/
}
